/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.nccAi.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import vip.xiaonuo.biz.modular.nccAi.param.*;
import vip.xiaonuo.common.annotation.CommonLog;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.biz.modular.nccAi.entity.BizAiNcc;
import vip.xiaonuo.biz.modular.nccAi.service.BizAiNccService;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * biz_ai_ncc控制器
 *
 * @author xsl
 * @date  2025/04/12 20:32
 */
@Tag(name = "biz_ai_ncc控制器")
@RestController
@Validated
public class BizAiNccController {

    @Resource
    private BizAiNccService bizAiNccService;

    /**
     * 获取用户单据选项
     *
     * @author xsl
     * @date  2025/06/17 20:32
     */
    @Operation(summary = "获取用户单据选项")
    @GetMapping("/biz/aincc/getNccOptions")
    public CommonResult<Map<String, List<String>>> getNccOptions() {
        return CommonResult.data(bizAiNccService.getNccOptions());
    }


    /**
     * 获取biz_ai_ncc分页
     *
     * @author xsl
     * @date  2025/04/12 20:32
     */
    @Operation(summary = "获取biz_ai_ncc分页")
    @SaCheckPermission("/biz/aincc/page")
    @GetMapping("/biz/aincc/page")
    public CommonResult<Page<BizAiNcc>> page(BizAiNccPageParam bizAiNccPageParam) {
        return CommonResult.data(bizAiNccService.page(bizAiNccPageParam));
    }

    /**
     * 添加biz_ai_ncc
     *
     * @author xsl
     * @date  2025/04/12 20:32
     */
    @Operation(summary = "添加biz_ai_ncc")
    @CommonLog("添加biz_ai_ncc")
    @SaCheckPermission("/biz/aincc/add")
    @PostMapping("/biz/aincc/add")
    public CommonResult<String> add(@RequestBody @Valid BizAiNccAddParam bizAiNccAddParam) {
        bizAiNccService.add(bizAiNccAddParam);
        return CommonResult.ok();
    }

    /**
     * 编辑biz_ai_ncc
     *
     * @author xsl
     * @date  2025/04/12 20:32
     */
    @Operation(summary = "编辑biz_ai_ncc")
    @CommonLog("编辑biz_ai_ncc")
    @SaCheckPermission("/biz/aincc/edit")
    @PostMapping("/biz/aincc/edit")
    public CommonResult<String> edit(@RequestBody @Valid BizAiNccEditParam bizAiNccEditParam) {
        bizAiNccService.edit(bizAiNccEditParam);
        return CommonResult.ok();
    }

    /**
     * 编辑biz_ai_ncc
     *
     * @author xsl
     * @date  2025/04/12 20:32
     */
    @Operation(summary = "编辑biz_ai_ncc")
    @CommonLog("编辑biz_ai_ncc")
    @SaCheckPermission("/biz/aincc/edit")
    @PostMapping("/biz/aincc/editBack")
    public CommonResult<String> editBack(@RequestBody @Valid BizAiNccAddParam addParam) {
        bizAiNccService.editBack(addParam);
        return CommonResult.ok();
    }

    /**
     * 删除biz_ai_ncc
     *
     * @author xsl
     * @date  2025/04/12 20:32
     */
    @Operation(summary = "删除biz_ai_ncc")
    @CommonLog("删除biz_ai_ncc")
    @SaCheckPermission("/biz/aincc/delete")
    @PostMapping("/biz/aincc/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                                   List<BizAiNccIdParam> bizAiNccIdParamList) {
        bizAiNccService.delete(bizAiNccIdParamList);
        return CommonResult.ok();
    }

    /**
     * 获取biz_ai_ncc详情
     *
     * @author xsl
     * @date  2025/04/12 20:32
     */
    @Operation(summary = "获取biz_ai_ncc详情")
    @SaCheckPermission("/biz/aincc/detail")
    @GetMapping("/biz/aincc/detail")
    public CommonResult<BizAiNcc> detail(@Valid BizAiNccIdParam bizAiNccIdParam) {
        return CommonResult.data(bizAiNccService.detail(bizAiNccIdParam));
    }

    /**
     * 获取biz_ai_ncc详情
     *
     * @author xsl
     * @date  2025/04/12 20:32
     */
    @Operation(summary = "上传文件")
    @SaCheckPermission("/biz/aincc/uploadFile")
    @GetMapping("/biz/aincc/uploadFile")
    public CommonResult<BizAiNcc> uploadFile(@Valid BizAiNccIdParam bizAiNccIdParam) {
        return CommonResult.data(bizAiNccService.detail(bizAiNccIdParam));
    }

    /**
     * 获取biz_ai_ncc详情
     *
     * @author xsl
     * @date  2025/04/12 20:32
     */
    @Operation(summary = "上传文件")
    @SaCheckPermission("/biz/aincc/createPayBill")
    @GetMapping("/biz/aincc/createPayBill")
    public CommonResult<String> createPayBill(@Valid BizAiNccIdParam bizAiNccIdParam) {
        BizAiNcc aiNcc = bizAiNccService.getById(bizAiNccIdParam.getId());
        bizAiNccService.createPayBill(aiNcc);
        return CommonResult.data("后台正在创建付款单");
    }

    /**
     * 获取biz_ai_ncc详情
     *
     * @author xsl
     * @date  2025/04/12 20:32
     */
    @Operation(summary = "查询收款合同")
    @GetMapping("/biz/aincc/searchIncome")
    public CommonResult<List<BizAiNccIncomeConReturnParam>> searchIncome(@Valid  @RequestParam("vbillcode") String vbillcode , @RequestParam("startTime") String startTime , @RequestParam("endTime") String endTime) {
        DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.ENGLISH);
        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 将字符串解析为ZonedDateTime
        ZonedDateTime start = ZonedDateTime.parse(startTime, inputFormatter.withZone(ZoneId.of("GMT")));
        ZonedDateTime end = ZonedDateTime.parse(endTime, inputFormatter.withZone(ZoneId.of("GMT")));
        // 转换为LocalDateTime（如果不需要时区信息）
        LocalDateTime startDate = start.toLocalDateTime();
        LocalDateTime endDate = end.toLocalDateTime();
        // 格式化为所需的输出格式
        String startString = startDate.format(outputFormatter);
        String endString = endDate.format(outputFormatter);
        return CommonResult.data(bizAiNccService.searchIncome(vbillcode, startString, endString));
    }


    /**
     * 创建NCC收款单据表单
     *
     * @author xsl
     * @date  2025/06/17 20:32
     */
    @Operation(summary = "查询收款合同")
    @PostMapping("/biz/aincc/createGatherBill")
    public CommonResult<String> createGatherBill(@Valid  @RequestBody BizAiNccGatherAddParam addParam) {
        List<String> nccKeyList = addParam.getNccKey();
        String[] contractList = addParam.getIncomeContract().split("-");
        String fcArPk = contractList[0];
        List<String> fcArPkBList = Arrays.asList(contractList[1].split(","));
        if(nccKeyList.isEmpty() || fcArPk.isEmpty()){
            return CommonResult.data("未选择表单参数");
        }
        return CommonResult.data(bizAiNccService.createGatherBill(nccKeyList, fcArPk, fcArPkBList, addParam.getVbillcode(), addParam.getMaterial()));
    }
    @Operation(summary = "批量下载")
    @CommonLog("批量下载")
    @PostMapping(value = "/biz/aincc/downloadZip", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public CommonResult<String> downloadZip(@RequestBody BizAiNccGatherAddParam addParam, HttpServletResponse response) throws IOException {
        bizAiNccService.getFilesList(addParam.getNccKey(), response);
        return CommonResult.ok();
    }
}
